package com.turbo.cloud.ums.utils;

import com.turbo.cloud.ums.web.entity.UmsDept;
import com.turbo.cloud.ums.web.entity.UmsMenu;
import com.turbo.cloud.ums.web.model.menu.UmsMenuVO;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MenuTreeUtil {

    /**
     * 菜单转菜单树，同时标记选中菜单
     *
     * @param menuSourceList 源菜单列表
     * @param menuIds        关联菜单ID
     * @return List<UmsMenuVO>
     */
    public static List<UmsMenuVO> toMenuCheckedTree(List<UmsMenu> menuSourceList, List<String> menuIds) {
        List<UmsMenuVO> trees = new ArrayList<>();
        for (UmsMenu treeNode : menuSourceList) {
            UmsMenuVO umsMenuVO = new UmsMenuVO();
            BeanUtils.copyProperties(treeNode, umsMenuVO);

            for (UmsMenu it : menuSourceList) {
                UmsMenuVO menuVO = new UmsMenuVO();
                BeanUtils.copyProperties(it, menuVO);

                if (Objects.nonNull(menuIds) && !menuIds.isEmpty()) {
                    menuVO.setChecked(menuIds.contains(it.getId()));
                } else {
                    menuVO.setChecked(false);
                }
                if (treeNode.getId().equals(it.getParentId())) {
                    if (CollectionUtils.isEmpty(umsMenuVO.getChildren())) {
                        umsMenuVO.setChildren(new ArrayList<>());
                    }
                    umsMenuVO.getChildren().add(menuVO);
                }
            }

            if ("0".equals(treeNode.getParentId())) {
                if ("0".equals(treeNode.getType() + "")) {
                    if (!CollectionUtils.isEmpty(umsMenuVO.getChildren())) {
                        trees.add(umsMenuVO);
                    }
                } else {
                    trees.add(umsMenuVO);
                }
            }
        }
        return trees;
    }

    /**
     * 菜单转选中菜单树
     *
     * @param menuSourceList 源菜单列表
     * @param menuIds        关联菜单ID
     * @return List<UmsMenuVO>
     */
    public static List<UmsMenuVO> toCheckedMenuTree(List<UmsMenu> menuSourceList, List<String> menuIds) {
        List<UmsMenuVO> trees = new ArrayList<>();
        for (UmsMenu treeNode : menuSourceList) {
            UmsMenuVO umsMenuVO = new UmsMenuVO();
            BeanUtils.copyProperties(treeNode, umsMenuVO);

            for (UmsMenu it : menuSourceList) {
                UmsMenuVO menuVO = new UmsMenuVO();
                BeanUtils.copyProperties(it, menuVO);

                if (Objects.nonNull(menuIds) && !menuIds.isEmpty()) {
                    menuVO.setChecked(menuIds.contains(it.getId()));
                    if (menuIds.contains(it.getId()) && treeNode.getId().equals(it.getParentId())) {
                        if (umsMenuVO.getChildren() == null) {
                            umsMenuVO.setChildren(new ArrayList<>());
                        }
                        umsMenuVO.getChildren().add(menuVO);
                    }
                } else {
                    menuVO.setChecked(false);
                }
            }

            if ("0".equals(treeNode.getParentId())) {
                if ("0".equals(treeNode.getType() + "")) {
                    if (!CollectionUtils.isEmpty(umsMenuVO.getChildren())) {
                        trees.add(umsMenuVO);
                    }
                } else {
                    trees.add(umsMenuVO);
                }
            }
        }
        return trees;
    }

    /**
     * 菜单转菜单树
     *
     * @param menuSourceList 源菜单列表
     * @return List<UmsMenuVO>
     */
    public static List<UmsMenuVO> toMenuTree(List<UmsMenu> menuSourceList) {
        if (Objects.isNull(menuSourceList) || menuSourceList.isEmpty()) {
            return null;
        }
        // 定义新的list
        List<UmsMenuVO> menuTargetList = new ArrayList<>();
        // 先找到所有的一级分类
        for (UmsMenu menuSource : menuSourceList) {
            // 一级菜单的parentId是0
            if ("0".equals(menuSource.getParentId())) {
                UmsMenuVO umsMenuVO = new UmsMenuVO();
                umsMenuVO.setStateValue(1 == menuSource.getStateId());
                BeanUtils.copyProperties(menuSource, umsMenuVO);
                menuTargetList.add(umsMenuVO);
            }
        }
        // 为一级菜单设置子菜单，getChild是递归调用的
        for (UmsMenuVO menuTarget : menuTargetList) {
            menuTarget.setChildren(getChildren(menuSourceList, menuTarget.getId()));
        }
        return menuTargetList;
    }

    /**
     * 递归查找子菜单
     *
     * @param menuSourceList 源菜单列表
     * @param id             当前菜单ID
     * @return
     */
    private static List<UmsMenuVO> getChildren(List<UmsMenu> menuSourceList, String id) {
        //子级
        List<UmsMenuVO> menuChildrenList = new ArrayList<>();
        for (UmsMenu menuSource : menuSourceList) {
            // 遍历所有节点，将父级id与传过来的id比较
            if (menuSource.getParentId().equals(id)) {
                UmsMenuVO umsMenuVO = new UmsMenuVO();
                umsMenuVO.setStateValue(1 == menuSource.getStateId());
                BeanUtils.copyProperties(menuSource, umsMenuVO);
                menuChildrenList.add(umsMenuVO);
            }
        }
        // 把子级的子级再循环一遍
        for (UmsMenuVO menuTarget : menuChildrenList) {
            menuTarget.setChildren(getChildren(menuSourceList, menuTarget.getId()));
        }
        // 递归退出条件
        if (menuChildrenList.size() == 0) {
            return null;
        }
        return menuChildrenList;
    }

    /**
     * 递归查找子菜单的父级ID
     *
     * @param menuSourceList 源菜单列表
     * @param menuIds        关联菜单ID列表
     * @return
     */
    public static List<String> getParentIds(List<UmsMenu> menuSourceList, List<String> menuIds) {
        List<String> parentIds = new ArrayList<>();
        for (String menuId : menuIds) {
            getParentId(menuSourceList, parentIds, menuId);
        }
        return parentIds;
    }

    /**
     * 递归查找子菜单
     *
     * @param menuSourceList 源菜单列表
     * @param parentIds      父级ID列表
     * @param id             菜单ID
     * @return
     */
    private static void getParentId(List<UmsMenu> menuSourceList, List<String> parentIds, String id) {
        for (UmsMenu menuSource : menuSourceList) {
            // 遍历所有节点，将id与传过来的id比较
            if (menuSource.getId().equals(id)) {
                if (!parentIds.contains(menuSource.getParentId())) {
                    parentIds.add(menuSource.getParentId());
                    getParentId(menuSourceList, parentIds, menuSource.getParentId());
                }
                break;
            }
        }
    }

    /**
     * 根据菜单父级Id找到菜单子级Id
     *
     * @param parentId   父级ID
     * @param sourceList 源岗位列表
     */
    public static void getChildrenMenuIds(String parentId, List<UmsMenu> sourceList, List<String> targetIds) {
        if(Objects.isNull(targetIds)) {
            targetIds = new ArrayList<>();
        }

        if(!targetIds.contains(parentId)) {
            targetIds.add(parentId);
        }

        for (UmsMenu umsMenu : sourceList) {
            if (parentId.equals(umsMenu.getParentId())) {
                targetIds.add(umsMenu.getId());
                getChildrenMenuIds(umsMenu.getId(), sourceList, targetIds);
            }
        }
    }
}
